package sm.codec;

import org.apache.mina.core.buffer.IoBuffer;

import sm.codec.exception.DecodeException;
import sm.codec.exception.EncodeException;
import sm.codec.type.Variable;
import sm.def.Type;

public class OffsetCodec extends NumberCodec {
	@Override
	public Object decode(IoBuffer buffer) throws DecodeException {

		Object value = super.decode(buffer, Type.OFFSET_TYPE);
		if (value instanceof Double) {
			String stringValue = String.valueOf((Double) value);
			value = new Variable(stringValue, Type.OFFSET_TYPE);
		}

		return value;
	}

	@Override
	public void encode(IoBuffer buffer, Object value) throws EncodeException {
		if (value instanceof Variable) {
			Variable variable = (Variable) value;
			value = Double.parseDouble(variable.getValue());
		}
		super.encode(buffer, value, Type.OFFSET_TYPE);

	}
}
